<?php
class MW_Simplenews_Block_Listnews extends Mage_Core_Block_Template 
{
	private $_itemPerPage = 2;
	private $_pageFrame = 8;
	private $_curPage =1;

	public function __construct(){
		parent::__construct();

		$id = $this->getRequest()->getParam('id');
		// $model = Mage::getModel('simplenews/category')->load($id);

		$listnews = Mage::getModel('simplenews/news')->getCollection()->addFieldToFilter('category_id', $id)->getData();
		$this->setListnews($listnews);

		$categoryName = $this->getCategoryName($id);
		$this->setCategory($categoryName);

	}

	public function _prepareLayout(){
		return  parent::_prepareLayout();
		// $pager = $this->getLayout()->createBlock('page/html_pager', 'simplenews.pagination.news')->setCollection($this->getCollection());
		// $this->setChild('pager', $pager);

		// return $this;
	}

	// get contento category by $id
	public function getCategoryName($id){
		$category = Mage::getModel('simplenews/category')->load($id)->getData();
		return $category['title'];
	}

	public function getPagerHtml(){

		return $this->getChildHtml('pager');
	}

	// pagination

	public function fgetCollection($collection = null) {

		if($collection !='null'){
			$page = $this->getRequest()->getParam('p')			;
			if($page) $this->_curPage = $page;

			$collection->setCurPage($this->_curPage);
			$collection->setPageSize($this->_itemPerPage);

			return $collection;
		}
	}

	// get Pager Html

	public function dgetPagerHtml($collection = 'null'){
		
		$html = false;
		if($collection =='null') return;

		if(count($collection) > $this->_itemPerPage){

			$curPage = $this->getRequest()->getParam('p');

			$temp_collection =  (int)(count($collection));
			$temp_perpage = (int)($this->_itemPerPage);

			$pager = ( $temp_collection / $temp_perpage);

			$temp = count($collection) % $this->_itemPerPage;
			

			$count = ($temp == 0) ? $pager : $pager + 1 ;

			$url = $this->getPagerUrl();

			$start = 1;
			$end = $this->_pageFrame;

			$html .='<ol>';

			if(isset($curPage) && $curPage != 1){
                $start = $curPage - 1;                                        
                $end = $start + $this->_pageFrame;
            }else{
                $end = $start + $this->_pageFrame;
            }
            if($end > $count){
                $start = $count - ($this->_pageFrame-1);
            }else{
                $count = $end-1;
            }

            for($i = $start; $i<=$count; $i++)
            {
                if($i >= 1){
                    if($curPage){
                        $html .= ($curPage == $i) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'p/'.$i.'">'. $i .'</a></li>';
                    }else{
                        $html .= ($i == 1) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'p/'.$i.'">'. $i .'</a></li>';
                    }
                }
                
            }
            
            $html .= '</ol>';
		}

		return $html;
	}


	// get Pager Url 

	public function getPagerUrl(){
		$cur_url = mage::helper('core/url')->getCurrentUrl();
        $new_url = preg_replace('/\&p=.*/', '', $cur_url);
        
        return $new_url;
	}

}